package edu.ccut.saturn.component.impl.node;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ccut.saturn.component.ComponentFactory;
import edu.ccut.saturn.component.ComponentManager;
import edu.ccut.saturn.component.IComponent;
import edu.ccut.saturn.component.ILogicContext;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.IParameterInfo;
import edu.ccut.saturn.component.impl.SaturnParameterInfo;
import edu.ccut.saturn.component.xml.ISaturnElement;

/**
 * 用于执行Action组件
 * 
 * @author 谷钰
 * 
 * @version 1.0 2008.8.20
 * 
 * @see AbstractNodeInfo
 * @see ILogicContext
 * @see IParameterContext
 * 
 */
public class ActionNodeInfo extends AbstractNodeInfo {

	private String key;

	private HttpServletRequest request;

	private HttpServletResponse response;

	/**
	 * 获得引用的子action组件的键值
	 * 
	 * @return
	 */
	public String getKey() {
		return key;
	}

	/**
	 * 设置引用的子action组件的键值
	 * 
	 * @param key
	 */
	public void setKey(String key) {
		this.key = key;
	}

	public ActionNodeInfo(HttpServletRequest request,
			HttpServletResponse response, String key) {
		
		super(null, key, "Action", null, null, null, null, "Action-noview");

		this.request = request;
		this.response = response;
		this.key = key;

		SaturnParameterInfo requetParameterInfo = new SaturnParameterInfo();
		requetParameterInfo.setClassType(request.getClass());
		requetParameterInfo.setName("request");
		requetParameterInfo.setId("1");
		requetParameterInfo.setValueKey("request");
		requetParameterInfo.setScope(IParameterInfo.SCOPE_REQUEST);
		requetParameterInfo.setPassingType(IParameterInfo.PASSING_IN);

		SaturnParameterInfo responseParameterInfo = new SaturnParameterInfo();
		responseParameterInfo.setClassType(response.getClass());
		responseParameterInfo.setName("response");
		responseParameterInfo.setValueKey("response");
		responseParameterInfo.setId("2");
		responseParameterInfo.setScope(IParameterInfo.SCOPE_REQUEST);
		responseParameterInfo.setPassingType(IParameterInfo.PASSING_IN);

		List<IParameterInfo> parameters = new ArrayList<IParameterInfo>();
		parameters.add(requetParameterInfo);
		parameters.add(responseParameterInfo);

		super.setParameterInfos(parameters);
	}

	/**
	 * 
	 * @param logicContext
	 *            逻辑上下文对象，记录结点运行时的上下文信息，传入null即可
	 * @throws Exception
	 */
	public void doExecute(ILogicContext logicContext) throws Exception {
		IParameterContext parameterContext = createParameterContext(logicContext);

		try {
			IComponent action = ComponentManager.getInstance().getComponent(
					this.key);

			ComponentManager.beforeExecuteComponent(this, parameterContext);
			action.execute(parameterContext);

		} finally {
			ComponentManager.afterExecuteComponent(this, parameterContext);
		}
	}

	@Override
	protected IParameterContext createParameterContext(
			ILogicContext logicContext) {

		IParameterContext parameterContext = ComponentFactory
				.createParameterContext();

		parameterContext.addParameter(this.request, HttpServletRequest.class);
		parameterContext.addParameter(this.response, HttpServletResponse.class);
		parameterContext.setLogicContext(logicContext);
		return parameterContext;
	}

	public ISaturnElement toSaturnElement() {
		return null;
	}

}
